package pl.enigmatic.test;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;

import pl.enigmatic.awt.Frames;
import pl.enigmatic.util.Exceptions;


public class ControllerFrame extends JFrame {

	/** */
	private static final long serialVersionUID = 1L;

	private static JPanel getPanel(INamedPanel inp) {
		try {
			return inp.getPanel();
		} catch (Exception e) {
			e.printStackTrace();
			JPanel panel = new JPanel(new BorderLayout());
			JTextPane area = new JTextPane();
			area.setText(Exceptions.toString(e));
			area.setEditable(false);
			area.setFont(area.getFont().deriveFont(17f));
			area.setForeground(Color.red);
			panel.add(area, BorderLayout.CENTER);
			return panel;
		}
	}

	public ControllerFrame(final INamedPanel... panels) {
		setTitle(getClass().getName());
		setLayout(new BorderLayout());
		if (panels.length > 1) {
			final JTabbedPane pane = new JTabbedPane();
			for (final INamedPanel panel : panels) {
				pane.addTab(panel.getName(), getPanel(panel));
			}
			add(pane, BorderLayout.CENTER);
		} else {
			add(getPanel(panels[0]), BorderLayout.CENTER);
		}
	}

	public static ControllerFrame createControllerFrame(final Action... actions) {
		return new ControllerFrame(new ButtonsControllerPanel(actions));
	}

	public static ControllerFrame createControllerFrame(final Action action, final String... captions) {
		return new ControllerFrame(new ButtonsControllerPanel(action, captions));
	}

	public static ControllerFrame showControllerFrame(final Action... actions) {
		return Frames.showTest(createControllerFrame(actions));
	}

	public static ControllerFrame showControllerFrame(final Action action, final String... captions) {
		return Frames.showTest(createControllerFrame(action, captions));
	}
}
